package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.BrandFirmSolutionResp;
import com.homedo.odin.solution.contract.pc.SSolutionDetailResp;
import com.homedo.odin.solution.contract.pc.SkuListResp;
import com.homedo.odin.solution.contract.pc.SolutionAddPcReq;
import com.homedo.odin.solution.contract.pc.SolutionEditPcReq;
import com.homedo.odin.solution.contract.pc.SolutionResp;
import com.homedo.odin.solution.contract.pc.USolutionDetailResp;
import com.homedo.odin.solution.contract.pc.UserDownloadAuthResp;
import com.homedo.odin.solution.contract.pc.UserSolutionDetailResp;
import com.homedo.odin.solution.contract.pc.UserSolutionResp;
import com.homedo.odin.solution.contract.wap.SolutionListResp;
import com.homedo.odin.solution.enumcode.solution.RankTypeEnum;
import com.homedo.odin.solution.model.dto.BrandFirmSolutionResponseBean;
import com.homedo.odin.solution.model.request.pc.brand.PcBrandSolutionListRequestBean;
import com.homedo.odin.solution.model.response.pc.solution.SolutionRankResponseBean;
import com.homedo.odin.solution.service.SolutionRankServiceImpl;
import com.homedo.odin.solution.service.SolutionServiceImpl;
import com.homedo.odin.solution.service.bizservice.pc.SolutionsBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注：后期和 SolutionController 合并
 *
 * @author: zhaoxudong
 * @date: 2018/4/4 11:45
 * @Description:
 */
@Api(description = "方案", tags = {"Solution"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/solution")
public class SolutionsController extends BaseController<SolutionsBizService> {

    @Autowired
    private SolutionRankServiceImpl solutionRankService;

    @Autowired
    private SolutionServiceImpl solutionService;

    @ApiOperation(value = "PC_获取方案信息列表", notes = "PC_获取方案信息列表", response = SolutionResp.class)
    @GetMapping
    public ResultData list(
            @ApiParam(value = "方案类型1:系统方案 2:品牌方案", required = false) @RequestParam(value = "solutionType", required = false) Integer solutionType,
            @ApiParam(value = "当前登录用户", required = false) @RequestParam(value = "uid", required = false) Integer uid,
            @ApiParam(value = "品牌id", required = false) @RequestParam(value = "brandId", required = false) Integer brandId,
            @ApiParam(value = "系统属性(一级系统属性)", required = false) @RequestParam(value = "sysAttr", required = false) Integer sysAttr,
            @ApiParam(value = "建筑业态", required = false) @RequestParam(value = "buildingType", required = false) Integer buildingType,
            @ApiParam(value = "我的(0:否 1:是)", required = false) @RequestParam(value = "isMy", required = false) Integer isMy,
            @ApiParam(value = "曝光类型 1：精品 2：普通", required = false) @RequestParam(value = "expType", required = false) Integer expType,
            @ApiParam(value = "发布日期(0:升序 1:降序)", required = false) @RequestParam(value = "shelveTimeSort", required = false) Integer shelveTimeSort,
            @ApiParam(value = "创建日期(0:升序 1:降序)", required = false) @RequestParam(value = "createTimeSort", required = false) Integer createTimeSort,
            @ApiParam(value = "下载数量(0:升序 1:降序)", required = false) @RequestParam(value = "downloadCountSort", required = false) Integer downloadCountSort,
            @ApiParam(value = "浏览数量(0:升序 1:降序)", required = false) @RequestParam(value = "viewCountSort", required = false) Integer viewCountSort,
            @ApiParam(value = "评论数量(0:升序 1:降序)", required = false) @RequestParam(value = "commentCountSort", required = false) Integer commentCountSort,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        SolutionResp resp = baseService
                .list(solutionType, uid, brandId, sysAttr, buildingType, isMy, expType, shelveTimeSort,
                        createTimeSort, downloadCountSort, viewCountSort, commentCountSort, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_根据关键字获取方案信息列表", notes = "PC_根据关键字获取方案信息列表", response = SolutionResp.class)
    @GetMapping("/list/{keyword}")
    public ResultData keyword(
            @ApiParam(value = "方案类型1:系统方案 2:品牌方案", required = false) @RequestParam(value = "solutionType", required = false) String solutionType,
            @ApiParam(value = "搜索关键字", required = true) @PathVariable(value = "keyword") String keyword,
            @ApiParam(value = "当前登录用户", required = false) @RequestParam(value = "uid", required = false) Integer uid,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        SolutionResp resp = baseService.keyword(solutionType, keyword, uid, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取免费方案列表", notes = "PC_获取免费方案列表", response = SolutionResp.class)
    @GetMapping("/free")
    public ResultData freeList(
            @ApiParam(value = "是否收费 0：不收费 1：收费", required = false) @RequestParam(value = "isFree", required = false) Integer isFree,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        SolutionResp resp = baseService.queryFreeSolution4Home(isFree, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取品牌商的方案列表", notes = "PC_获取品牌商的方案列表", response = BrandFirmSolutionResp.class)
    @GetMapping("/brand_firm")
    public ResultData queryBrandFirmSolution(
            @ApiParam(value = "品牌厂商id", required = true) @RequestParam("uid") Integer uid,
            @ApiParam(value = "当前用户登陆Id", required = false) @RequestParam(value = "loginUserId", required = false) Integer loginUserId,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        BrandFirmSolutionResp resp = baseService.queryBrandFirmSolution(uid, loginUserId, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_查看用户是有权限下载", notes = "PC_查看用户是有权限下载", response = UserDownloadAuthResp.class)
    @GetMapping("/{solutionId}/auth")
    public ResultData auth(@ApiParam(value = "用户id", required = true) @RequestParam("uid") Integer uid,
            @ApiParam(value = "方案id", required = true) @PathVariable("solutionId") Long solutionId) {
        UserDownloadAuthResp resp = baseService.auth(uid, solutionId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取用户方案列表", notes = "PC_获取用户方案列表", response = UserSolutionResp.class)
    @GetMapping("/{userId}/list")
    public ResultData userSolList(@ApiParam(value = "当前登录用户") @PathVariable("userId") Integer userId,
            @ApiParam(value = "方案名称") @RequestParam(value = "solutionName", required = false) String solutionName,
            @ApiParam(value = "状态 0:已保存 10:审核中  20:审核通过 21:审核驳回 30:上架中 31:下架中") @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(value = "是否允许下载 0:不允许 1:允许") @RequestParam(value = "isAdmitDownload", required = false) Integer isAdmitDownload,
            @ApiParam(value = "是否收费 0：不收费 1：收费") @RequestParam(value = "isFree", required = false) Integer isFree,
            @ApiParam(value = "曝光类型 0:免费 1：精品 2：普通") @RequestParam(value = "expType", required = false) Integer expType,
            @ApiParam(value = "当前页码", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        UserSolutionResp res = baseService
                .userSolList(userId, solutionName, status, isAdmitDownload, isFree, expType, pageNum, pageSize);
        return ResultData.newInstance(res);
    }

    @ApiOperation(value = "PC_获取方案详情与收益", notes = "PC_获取方案详情与收益", response = UserSolutionDetailResp.class)
    @GetMapping("/earning/{solutionId}")
    public ResultData earning(@ApiParam("方案id") @PathVariable("solutionId") Long solutionId) {
        UserSolutionDetailResp resp = baseService.earning(solutionId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取方案热榜列表", notes = "PC_获取方案热榜列表", response = SolutionListResp.class)
    @GetMapping("/hot")
    public ResultData hotList(@ApiParam("类型 1：昨日热榜 2：周热榜 3：月热榜") @RequestParam("type") Integer type,
            @RequestParam(value = "number", defaultValue = "10") Integer number) {
        SolutionListResp resp = baseService.hotList(type, number);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取方案详细信息", notes = "PC_获取方案详细信息", response = SSolutionDetailResp.class)
    @GetMapping("/{solutionId}")
    public ResultData detail(@ApiParam(value = "方案id", required = true) @PathVariable("solutionId") Long solutionId,
            @ApiParam(value = "用户Id", required = false) @RequestParam(value = "userId", required = false) Integer userId,
            @ApiParam(value = "会员等级", required = false) @RequestParam(value = "memberLevel", required = false) Integer memberLevel) {
        SSolutionDetailResp resp = baseService.detail(solutionId, userId, memberLevel);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_用户查看方案详细信息", notes = "PC_用户查看方案详细信息", response = USolutionDetailResp.class)
    @GetMapping("/{userId}/{solutionId}")
    public ResultData basicDetail(@PathVariable("userId") Integer userId, @PathVariable("solutionId") Long solutionId) {
        USolutionDetailResp resp = baseService.basicDetail(userId, solutionId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_获取方案相关sku", notes = "PC_获取方案相关sku", response = SkuListResp.class)
    @GetMapping("/{solutionId}/skus")
    public ResultData skus(@PathVariable("solutionId") Long solutionId) {
        SkuListResp resp = baseService.productSkus(solutionId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "PC_新增方案基础信息", notes = "PC_新增方案基础信息")
    @PostMapping
    public ResultData add(@RequestBody SolutionAddPcReq req) {
        baseService.add(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "PC_编辑方案基础信息", notes = "PC_编辑方案基础信息")
    @PutMapping
    public ResultData edit(@RequestBody SolutionEditPcReq req) {
        baseService.edit(req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "PC_精品热搜榜", notes = "PC_精品热搜榜", response = SolutionRankResponseBean.class)
    @GetMapping("/rank/{biztype}/{dataSize}")
    public ResultData hotSearchAndBoutique(@ApiParam("类型 1： 热搜，2：精品") @PathVariable("biztype") Integer biztype,
            @ApiParam("获取数据条数") @PathVariable("dataSize") Integer dataSize) {
        if (!RankTypeEnum.dbValueExist(biztype)) {
            throw new BizException(Meta.CODE_451.getCode(), "bizytpe参数错误  （1： 热搜，2：精品）");
        }

        if (!NumberHelper.validate(dataSize)) {
            throw new BizException(Meta.CODE_451.getCode(), "dataSize参数非法");
        }

        List<SolutionRankResponseBean> list = solutionRankService.hotSearchAndBoutique(biztype, dataSize);
        return ResultData.newInstance(Meta.CODE_200, list);
    }

    @GetMapping("/brand_firm/{uid}")
    @ApiOperation(value = "PC_获取品牌主的方案列表", response = BrandFirmSolutionResponseBean.class, httpMethod = "GET")
    public ResultData getBrandFirmSolution(@PathVariable("uid") Integer uid,
            @Valid @ModelAttribute PcBrandSolutionListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultData.newInstance(new Meta(Meta.CODE_451.getCode(),
                    bindingResult.getFieldError().getDefaultMessage()));
        }
        if (!NumberHelper.validatePositive(uid)) {
            throw new BizException(Meta.CODE_451.getCode(), "用户 ID 参数非法");
        }
        return ResultData.newInstance(solutionService.getBrandFirmSolutions(uid, requestBean));
    }

}

